<!-- 增加打款记录 -->
<template>
  <div>
    <el-dialog
      width="900px"
      class="ship-record-dialog"
      :append-to-body="true"
      top="6vh"
      title="增加打款记录"
      :visible="$store.state.dialog.shipRecord"
      @close="close">
      <el-row>
        <el-col :span="24">
          <el-form :model="order" :rules="rules" ref="order" label-width="100px" size="mini">
            <el-row>
              <el-col :span="12">
                <el-form-item label="客户/用箱人" prop="shipAgencyId">
                  <el-select size="mini" filterable v-model="order.shipAgencyId" placeholder="请选择船代">
                  <el-option v-for="item in userInfo" :key="item.ID" :label="item.text" :value="item.shipAgencyId"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="打款金额" prop="money">
                  <el-input v-model="order.money"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" v-model="order.remark"></el-input>
              </el-form-item>
            </el-row>
            <el-row>
              <el-form-item label="相关图像">
                <div class="uploader">
                  <el-upload
                    action="http://fx.api.ypt56.net/api/image"
                    :show-file-list="true"
                    :data="uploadimgData"
                    :multiple="false"
                    name="Picture"
                    v-model="order.image"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload">
                    <img v-if="order.image" :src="order.image" class="avatar">
                    <i v-if="!order.image" class="el-icon-plus avatar-uploader-icon"></i>
                  </el-upload>
                </div>
              </el-form-item>
            </el-row>
          </el-form>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="loadShow" @click="syreAdd('order')" size="mini">确 定</el-button>
        <el-button type="primary" size="mini" v-else :loading="true">加载中</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addMoneyMaintain
} from '../api/finance'
export default {
  data () {
    var checkMoney = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('金额不能为空'))
      } else {
        const reg = /^[1-9]+\d*(\.\d{0,2})?$|^0?\.\d{0,2}$/
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入格式正确的金额'))
        }
      }
    }
    return {
      order: {
        shipAgencyId: '',
        money: '',
        remark: '',
        image: ''
      },
      loadShow: true,
      rules: {
        shipAgencyId: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        money: [
          { required: true, validator: checkMoney, trigger: 'blur' }
        ]
      }
    }
  },
  props: {
    userInfo: {
      type: Array,
      default: () => {
        return []
      }
    },
    uploadimgData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  methods: {
    close () {
      this.$store.commit('hide_ship_record_dialog')
    },
    // 添加客户打款维护、船代打款维护
    async getYardComList () {
      this.loadShow = false
      let that = this
      const res = await addMoneyMaintain({
        shipAgencyId: this.order.shipAgencyId, // type为0传船代编号 type为1传客户编号
        money: this.order.money, // 金额
        remark: this.order.remark, // 备注
        type: 0, // 船代打款维护 1 客户打款维护
        image: this.order.image // 图片地址
      })
      if (res.status === 200) {
        this.$notify({
          title: '成功',
          message: res.msg,
          type: 'success'
        })
        setTimeout(function () {
          that.loadShow = true
        }, 500)
        this.$parent.getCustomerMoney(1, 15)
        this.close()
      } else {
        this.$notify({
          title: '错误',
          message: res.msg,
          type: 'error'
        })
        this.loadShow = true
      }
    },
    beforeAvatarUpload (file) {
      this.uploadimgData.image = file.name.split('.')[0]
    },
    handleAvatarSuccess (res, file) {
      this.order.image = res.data[0].path
    },
    // 确认添加
    syreAdd (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.getYardComList()
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.ship-record-dialog {
  .uploader {
    width: 178px;
    height: 178px;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .uploader:hover {
    border-color: #409EFF;
  }
  .uploader /deep/ .el-upload__input{
    display: none;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
}
</style>
